package cn.life.business.notice.service.impl;

import cn.life.business.notice.entity.po.NoticeUserPo;
import cn.life.business.user.dao.UserDao;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.common.entity.model.CMessage;
import cn.life.business.notice.dao.NoticeDao;
import cn.life.business.notice.entity.po.Notice;
import cn.life.business.notice.entity.vo.NoticeVo;
import cn.life.business.notice.service.NoticeService;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.entity.vo.PushVo;
import cn.life.common.push.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:   公告实现层
 * @author wangzhenyu
 * @Date 2018/7/11
 */
@Service
@Transactional
public class NoticeServiceImpl  implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Autowired
    private UserDao userDao;

    @Override
    public CMessage addNotice(Notice notice) {
        if(notice.getUserList().size() > 0){
            Long noticeId = noticeDao.addNotice(notice);
            notice.setNoticeId(noticeId);
            noticeDao.addNoticeDetail(notice);

            StringBuffer userIds = new StringBuffer();
            String userId = "";
            for(NoticeUserPo po : notice.getUserList()){
                userIds.append(po.getUserId() + ",");
            }
            if(userIds.toString().length() > 0){
                userId = userIds.substring(0,userIds.length()-1);
                List<Map<String,Object>> mapList = userDao.queryUserList(userId);

                try {
                    new PushService().allPush("公告","您有新的公告，请注意查收",mapList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else{
            return CMessage.fail("请选择接收人员");
        }

        return CMessage.addSuccess();
    }

    @Override
    public CMessage deleteNotice(long noticeId) {
        return noticeDao.deleteNotice(noticeId) == 1 ? CMessage.success() : CMessage.fail("！");
    }

    @Override
    public CMessage updateNotice(Notice notice) {
        int res = noticeDao.updateNotice(notice);
        if(res == 0){
            return CMessage.modifyFail();
        }
        noticeDao.deleteNoticeDetail(notice.getNoticeId());
        if(notice.getUserList() != null){
            noticeDao.addNoticeDetail(notice);
        }
        return CMessage.modifySuccess();
    }

    @Override
    public CMessage getAllNoticeInfo(NoticeVo noticeVo) {
        Pagination<NoticeVo> list=noticeDao.getAllNoticeInfo(noticeVo);
        return CMessage.successByData(list);
    }

    @Override
    public CMessage getNotice(NoticeVo noticeVo,MyUserInfo myUserInfo) {
        //1.获取主表信息
        noticeVo=noticeDao.getNotice(noticeVo);
        //2.获取明细信息
        noticeVo.setUserList(noticeDao.getNoticeDetail(noticeVo));
        for(NoticeUserPo userPo : noticeVo.getUserList()){
            if(userPo.getUserId().equals(myUserInfo.getUserId()) && userPo.getState() == 0){
                //3.修改查询状态
                noticeDao.updateNoticeReadState(noticeVo,myUserInfo);

                int state = 11;
                String pushMark = "";
                //查询发布人的信息
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(noticeVo.getOprUserId());
                userInfo = userDao.getUserInfo(userInfo);
                if(myUserInfo.getJgPushMark() == null || myUserInfo.getJgPushMark().equals("")){
                    pushMark = userInfo.getHwPushMark();
                    state = 12;
                }else{
                    pushMark = userInfo.getJgPushMark();
                }

                PushVo pushVo = new PushService().pushVoInit("标题:",noticeVo.getNoticeTitle() + "，" + myUserInfo.getUserName() + "：已读",state,pushMark);

                try {
                    new PushService().push(pushVo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return CMessage.successByData(noticeVo);
    }

    @Override
    public CMessage updateNoticeState(NoticeUserPo noticeUserPo) {
        return noticeDao.updateNoticeState(noticeUserPo)==1?CMessage.success() : CMessage.fail("修改公告状态失败！");
    }
}
